import pygame, sys, os
from pygame.locals import *

##############################################################################
#Enemy Class
##############################################################################

class Enemy(pygame.sprite.Sprite):
	"""
	This class can be used to create an enemy object. Functions and notes are as follows:
	
	__init__
		defines initial variables used within the class
	
	loadimages
		loads each image for the enemy animation
		an error message will be brought up if correct images are not found
	
	update
		use RenderUpdates()
		updates animation frame each time update is called
		takes the following parameters
			currentTime	
				obtained from pygame.time.get_ticks()
			enemyMinx
				obtained from level class
			enemyMaxX
				obtained from level class
			enemyDown
				obtained from level class
			moveSpeed
				obtained from level class
	move
		controls the enemy path
		takes the following parameters
			left
				obtained from update function via enemyMinx
			right
				obtained from update function via enemyMaxX
			down
				obtained from update function via enemyDown
			speed
				obtained from update function via moveSpeed
	"""
	image = None
	
	#########################################
	#Set inital variables
	#########################################
	
	def __init__(self, initialPosition):
		pygame.sprite.Sprite.__init__(self)
	
		
		
		if Enemy.image is None:
			self.loadImages()
            # This is the first time this class has been instantiated.
            # So, load the image for this and all subsequence instances
			self.image = self.temp
			self.rect = self.image.get_rect()
			self.rect.topleft = initialPosition
			self.frame = 0
			self.nextUpdateTime = 0
			self.goingRight =  True
			self.goingLeft =  False
			self.goingDownLeft = False
			self.goingDownRight = False
			self.downTempL = 0
			self.downTempR = 0
			

	#########################################
	#loadImages Function
	#Loads images for enemy animation
	#########################################
	
	def loadImages(self):
		self.enemy1 = []
		
		try:
			#frame 1
			a1f1 = os.path.join("image","a1f1.gif")
			self.temp = pygame.image.load(a1f1).convert()
			key = self.temp.get_at((1, 1)) #find pix and use as trans colour
			self.temp.set_colorkey(key)
		
			#add frame
			self.enemy1.append(self.temp)
		
			#frame 2
			a1f2 = os.path.join("image","a1f2.gif")
			self.temp = pygame.image.load(a1f2).convert()
			key = self.temp.get_at((1, 1))
			self.temp.set_colorkey(key)
		
			#add frame
			self.enemy1.append(self.temp)
		
			#frame 3
			a1f3 = os.path.join("image","a1f3.gif")
			self.temp = pygame.image.load(a1f3).convert()
			key = self.temp.get_at((1, 1))
			self.temp.set_colorkey(key)
		
		except pygame.error:
			#elaborate error message
			self.Font = pygame.font.Font(None, 20)   
			self.textOpen = self.Font.render("Images totally cant be found. Closing...", 1, (255, 255, 255))
			self.textOpenPos = self.textOpen.get_rect()
			self.textOpenPos.centerx = background.get_rect().centerx
			self.textOpenPos.centery = background.get_rect().centery

			screen.blit(self.textOpen, self.textOpenPos)  
			pygame.display.update()
			pygame.time.delay(2000)
			sys.exit(0)
		
		#add frame
		self.enemy1.append(self.temp)
	
	#########################################
	#Update function. Used with RenderUpdates()
	#########################################
	
	def update(self, currentTime, enemyMinx, enemyMaxX, enemyDown, moveSpeed):	#pygame.time.get_ticks(), move function
		if self.nextUpdateTime < currentTime:
			#moves to the next frame, resets if there are no more
			self.frame += 1
			if self.frame >= len(self.enemy1):
				self.frame = 0
			self.image = self.enemy1[self.frame]
			self.move(enemyMinx, enemyMaxX, enemyDown, moveSpeed)

		self.nextUpdateTime = currentTime + 10
		
		
	#########################################
	#Move function
	#########################################
	
	def move(self, left, right, down, speed):
		
		for count in range(speed): #used to differ from animation speed

			#########################################
			#Which way to go
			#########################################			
			
			if self.rect.right == right - 1:
				self.goingRight = False
				self.goingLeft = False
				self.goingDownRight = True
				self.goingDownLeft = False
				
			if self.rect.left == left + 1:
				self.goingRight = False
				self.goingLeft = False
				self.goingDownRight = False
				self.goingDownLeft = True
	
			if self.downTempL == down + 1:
				self.downTempL = 0
				self.goingRight = True
				self.goingLeft = False
				self.goingDownRight = False
				self.goingDownLeft = False
			
			if self.downTempR == down + 1:
				self.downTempR = 0
				self.goingRight = False
				self.goingLeft = True
				self.goingDownRight = False
				self.goingDownLeft = False
				
			#########################################
			#Movement
			#########################################
			
			if self.goingDownLeft:
				self.rect.bottom += 1
				self.downTempL += 1
				
				
			if self.goingDownRight:
				self.rect.bottom += 1
				self.downTempR += 1

			
			if self.goingRight:
				self.rect.left +=1
			
			if self.goingLeft:
				self.rect.right -=1
				
		

##############################################################################	
#display
##############################################################################

pygame.init()

#Screen size
screenWidth = 300
screenHeight = 300


screen = pygame.display.set_mode([screenWidth, screenHeight])


pygame.display.set_caption('Enemy Testing. Jelly Fish Are Cool')

################
#background
####################

background = pygame.Surface([screenWidth, screenHeight])
background.fill([255,0,0])
screen.blit(background, [0,0])

###########################
#add those bad guys
#state initial position
#
#x min of 2
#y min of 2 * surface (32)
#
#be careful with enemy positions and variables unless you want enemies to overlap
#
#stick this in a level class
############################

enemyMinx = 0
enemyMaxX = screenWidth
enemyDown = 32
moveSpeed = 3

enemy1 = pygame.sprite.RenderUpdates()

for position in [
	([2,0]),
	([100,0]),
	([150,0]),
	([200,0]),
	([250,0]),
	([2,64]),
	([50,64]),
	([100,64]),
	([150,64]),
	([200,64]),
	([250,64]),
	([2,0]),
	([50,0]),
	([100,0]),
	([150,0]),
	([200,0]),
	([250,0]),
	([2,128]),
	([50,128]),
	([100,128]),
	([150,128]),
	([200,128]),
	([250,128])
	]:

	enemy1.add(Enemy(position))

#######
#Quit
########

def input(events):
	for event in events:
		if event.type == QUIT:
			sys.exit(0)

####################
#main game flow
####################

while pygame.event.poll().type != KEYDOWN:
	input(pygame.event.get()) 
	
	#save time by calling this only once
	#slam in level variables here. xmin, xmax and down movement, enemy speed
	enemy1.update(pygame.time.get_ticks(), enemyMinx,enemyMaxX,enemyDown, moveSpeed) 

	
	rectlist = enemy1.draw(screen)
	pygame.display.update(rectlist)

	pygame.time.delay(75)
	enemy1.clear(screen, background)


